home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / mordor_2.000 / mordor_2 / src / finger.c < prev    next >
C/C++ Source or Header  |  1995-06-21  |  2KB  |  100 lines

  1. /*
  2.  * FINGER.C:
  3.  *
  4.  *    Remote finger functions
  5.  *
  6.  *    Copyright (C) 1992, 1993 Brett J. Vickers
  7.  *
  8.  */
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/time.h>
  14. #include <sys/signal.h>
  15. #include <ctype.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>
  18. #include <netdb.h>
  19. #include <errno.h>
  20. #include <stdio.h> 
  21.  
  22. #define write_fd(a,b)    write(a, b, strlen(b));
  23.  
  24. main(argc, argv) 
  25. int    argc;
  26. char    *argv[];
  27. {
  28.     void finger();
  29.     int fd;
  30.  
  31.     if(argc < 3) exit(0);
  32.  
  33.     fd = atoi(argv[1]);
  34.     if(argc == 4)
  35.         finger(fd, argv[2], argv[3]);
  36.     else
  37.         finger(fd, argv[2], "");
  38. }
  39.  
  40. /************************************************************************/
  41. /*                finger                    */
  42. /************************************************************************/
  43.  
  44. void finger(fd, address, person)
  45. int    fd;
  46. char    *address;
  47. char    *person;
  48. {
  49.     int            new_sock, n, Tablesize;
  50.     char            buf[80];
  51.     unsigned long        addr;
  52.     struct sockaddr_in    sin;
  53.     struct hostent        *h;
  54.     fd_set            mask;
  55.  
  56.     if((addr = inet_addr(address)) != -1)
  57.         sin.sin_family = AF_INET;
  58.  
  59.     else if((h = gethostbyname(address)) != NULL) {
  60.         sin.sin_family = h->h_addrtype;
  61.         bcopy(h->h_addr, &addr, h->h_length);
  62.     }
  63.  
  64.     else {
  65.         write_fd(fd, "\n\rInvalid host address\n\r");
  66.         exit(0);
  67.     }
  68.  
  69.     sin.sin_addr.s_addr = addr;
  70.     sin.sin_port = htons(79);
  71.  
  72.     if((new_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  73.         write_fd(fd, "\n\rUnable to connect to finger server.\n\r");
  74.         exit(0);
  75.     }
  76.  
  77.     if(connect(new_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  78.         write_fd(fd, "\n\rUnable to connect to finger server.\n\r");
  79.         exit(0);
  80.     }
  81.  
  82.     write(new_sock, person, strlen(person));
  83.     write(new_sock, "\n\r\n\r", 4);
  84.  
  85.     FD_ZERO(&mask);
  86.  
  87.     Tablesize = getdtablesize();
  88.  
  89.     while(1) {
  90.         FD_SET(new_sock, &mask);
  91.         select(Tablesize, &mask, 0, 0, 0);
  92.         n = read(new_sock, buf, 80);
  93.         if(n <= 0) {
  94.             close(new_sock);
  95.             exit(0);
  96.         }
  97.         write(fd, buf, n);
  98.     }
  99. }
  100.